# -*-coding utf-8 -*-
__author__ = 'aaqqxx'

def is_all_between(data,smallest,biggest):
    for each in data:
        if each >=smallest and each<=biggest:
            value = True
        else:
            value = False
            break
    return value

def get_setting_time(data,cmd_position,time_tick = 2,time_unit = 'ms',setting_time_factor = 0.05,is_cal_all = True):
    setting_time = -1
    position_setting_biggest = cmd_position*(1+setting_time_factor)
    position_setting_smallest = cmd_position*(1-setting_time_factor)
    if is_cal_all:
        for i,each in enumerate(data):
            if (data[i]>= position_setting_smallest and data[i] <= position_setting_biggest): #record the first point according to the condition
                if is_all_between(data[i:],position_setting_smallest,position_setting_biggest):
                    setting_time = i*time_tick
                    break
            else :
                setting_time = -1
    else:
        data = data[:len(data)/2]
        for i,each in enumerate(data): #only use the half of the data to calculate the setting_time.
            if (data[i]>= position_setting_smallest and data[i] <= position_setting_biggest):#record the first point according to the condition
                if is_all_between(data[i:],position_setting_smallest,position_setting_biggest):
                    setting_time = i
                    break
            else :
                setting_time = -1
    return setting_time

def get_raise_time(data, cmd_position,time_tick = 1, time_unit="ms",raise_time_factor = 0.7):
    if time_unit == "ms":   #if time_unit is "ms",the time_factor
        time_tick = 1
    for i,each in enumerate(data):
        if (data[i] >= cmd_position*raise_time_factor):
            raise_time= i*time_tick
            break
        else :
            raise_time = -1
    return raise_time

def get_peak_time(data,cmd_position,time_tick =2 ,time_unit = 'ms'):
    if time_unit == "ms":   #if time_unit is "ms",the time_factor
        time_tick = time_tick

    for i,each in enumerate(data):
        if (data[i] >= cmd_position):
            peak_time= i*time_tick
            break
        else :
            peak_time = -1
    return peak_time



if __name__=='__main__':
    data = [0,1,2,3,4,5,6]
    peak_time = get_peak_time(data,4)
    print peak_time